"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

from msnp.service import utils

class ReliableTransportType:
    """The different types of reliable transport messages."""
    FINISHCRASH = 'finishCrash'
    SEARCHSUCCESSOR = 'searchSuccessor'
    REHELLO = 'rehello'
    LEAVE = 'leave'    
    JOIN = 'join'
    DELETE_BACKUP = 'delete_backup'

#####################
##   SUPER_CLASS   ##
#####################

class ReliableTransportMessage():
    """The super-class for all reliable transport messages.
    
    Arguments:
        __timestamp: the timestamp
        __type: the type, defined by the class ReliableTransportType
        __compareIdentity: a needed PeerInfo instance (usage depends on the type)
        __useBroadcast: boolean determining whether to use a broadcast or not
    """
    
    def __init__(self, type, compareIdentity, useBroadcast=False):
        self.__timestamp = utils.DateFormatter.formatTimeAsFloat()
        self.__type = type
        self.__compareIdentity = compareIdentity
        self.__useBroadcast = useBroadcast
        
    def getType(self):
        return self.__type

    def getTimestamp(self):
        return self.__timestamp
    
    def getCompareIdentity(self):
        return self.__compareIdentity
    
    def isUseBroadcast(self):
        return self.__useBroadcast
    
    def setUseBroadcast(self, useBroadcast):
        self.__useBroadcast = useBroadcast
        
    def isEqual(self, rtMessage):
        """This method determines whether a given reliable transport message is identical to the current one.
        
        Args:
            rtMessage: a ReliableTransportMessage instance
            
        Returns:
            True if the given ReliableTransporMessage instance is identical, False otherwise
        """
        return self.getType() == rtMessage.getType() and self.getTimestamp() == rtMessage.getTimestamp() and rtMessage.getCompareIdentity().getName() == self.getCompareIdentity().getName()
        
    
#####################
##   SUB_CLASSES   ##
#####################

class RTFinishCrashMessage(ReliableTransportMessage):
    """Reliable transport message for the crash process.
    
    Attributes:
        __successor: the successor (PeerInfo instance)
        __predecessor: the predecessor (PeerInfo instance)
        __oldPredecessorOfSuccessor: the old predecessor of the successor (PeerInfo instance)
    """
    
    def __init__(self, predecessor, successor, oldPredecessorOfSuccessor):
        ReliableTransportMessage.__init__(self, ReliableTransportType.FINISHCRASH, predecessor)
        self.__successor = successor
        self.__predecessor = predecessor
        self.__oldPredecessorOfSuccessor = oldPredecessorOfSuccessor
    
    def getSuccessor(self):
        return self.__successor
    
    def getPredecessor(self):
        return self.__predecessor
    
    def getOldPredecessorOfSuccessor(self):
        return self.__oldPredecessorOfSuccessor
    
    
class RTSearchSuccessorMessage(ReliableTransportMessage):
    """Reliable transport message for the search of the successor of a crashed peer.
    
    Attributes:
        __crashedIdentity: the crashed peer (PeerInfo instance)
        __predecessorOfCrashedIdentity: the predecessor of the crashed peer (PeerInfo instance)
    """
    
    def __init__(self, crashedIdentity, predecessorOfCrashedIdentity):
        ReliableTransportMessage.__init__(self, ReliableTransportType.SEARCHSUCCESSOR, predecessorOfCrashedIdentity)
        self.__crashedIdentity = crashedIdentity
        self.__predecessorOfCrashedIdentity = predecessorOfCrashedIdentity
    
    def getCrashedIdentity(self):
        return self.__crashedIdentity
    
    def getPredecessorOfCrashedIdentity(self):
        return self.__predecessorOfCrashedIdentity
    
    
class RTRehelloMessage(ReliableTransportMessage):
    """Reliable transport message for the re-hello process during a crash.
    
    Attributes:
        __fromIdentity: the starting peer that has to re-hello (PeerInfo instance)
        __toIdentity: the last peer that has to re-hello (PeerInfo instance)
    """
    
    def __init__(self, fromIdentity, toIdentity):
        ReliableTransportMessage.__init__(self, ReliableTransportType.REHELLO, toIdentity)
        self.__fromIdentity = fromIdentity
        self.__toIdentity = toIdentity
    
    def getFromIdentity(self):
        return self.__fromIdentity
    
    def getToIdentity(self):
        return self.__toIdentity
    
    
class RTLeaveMessage(ReliableTransportMessage):
    """Reliable transport message for the leave process.
    
    Attributes:
        __leavedIdentity: the leaving peer (PeerInfo instance)
        __predecessor: the predecessor of the leaving (PeerInfo instance)
        __successor: the successor of the leaving (PeerInfo instance)
    """
    
    def __init__(self, leavedIdentity, predecessor, successor):
        ReliableTransportMessage.__init__(self, ReliableTransportType.LEAVE, leavedIdentity)
        self.__leavedIdentity = leavedIdentity
        self.__predecessor = predecessor
        self.__successor = successor
    
    def getPredecessor(self):
        return self.__predecessor
    
    def getSuccessor(self):
        return self.__successor
    
    def getLeavedIdentity(self):
        return self.__leavedIdentity
    
    
class RTJoinMessage(ReliableTransportMessage):
    """Reliable transport message for the join process.
    
    Attributes:
        __joinIdentity: the joining peer (PeerInfo instance)
    """
    
    def __init__(self, joinIdentity):
        ReliableTransportMessage.__init__(self, ReliableTransportType.JOIN, joinIdentity)
        self.__joinIdentity = joinIdentity
    
    def getJoinIdentity(self):
        return self.__joinIdentity
    
    
class RTDeleteBackupMessage(ReliableTransportMessage):
    """Reliable transport message for the backup deletion.
    
    Attributes:
        __senderIdentity: the sender peer (PeerInfo instance)
        __backupFromUsersToDelete: a list of backup sources, that have to be deleted.
    """
    
    def __init__(self, senderIdentity, backupFromUsersToDelete):
        ReliableTransportMessage.__init__(self, ReliableTransportType.DELETE_BACKUP, senderIdentity)
        self.__senderIdentity = senderIdentity
        self.__backupFromUsersToDelete = backupFromUsersToDelete
    
    def getSenderIdentity(self):
        return self.__senderIdentity
    
    def getBackupFromUsersToDelete(self):
        return self.__backupFromUsersToDelete
    
